package com.twetec.cms.adapter.pc.backend.controller.statistic;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.twetec.cms.adapter.pc.backend.response.R;
import com.twetec.cms.model.po.statistic.StatisticSitePageViewsPO;
import com.twetec.cms.model.po.website.SitePO;
import com.twetec.cms.model.vo.statistic.BarChartListVO;
import com.twetec.cms.model.vo.statistic.BarChartVO;
import com.twetec.cms.model.vo.statistic.LineChartListVO;
import com.twetec.cms.service.statistic.StatisticSitePageViewsService;
import com.twetec.cms.service.website.SiteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: zhaichangzheng
 * @Date: 2022/3/5 10:59 （日期和时间）
 */
@Api(tags = "统计栏目")
@RestController
@RequestMapping("/statistic/site")
public class StatisticSitePageViewCountController {

    @Autowired
    private StatisticSitePageViewsService sitePageViewsService;

    @Autowired
    private SiteService siteService;

    @ApiOperation(value = "站点访问量统计", notes = "站点访问量统计")
    @GetMapping("/site-view-count")
    public R<BarChartListVO> siteViewCount(
            @ApiParam(value = "开始时间（10位时间戳）", required = false, example = "1234567891") @RequestParam(required = false) Date gmtStartTime,
            @ApiParam(value = "结束时间（10位时间戳）", required = false, example = "1231231231") @RequestParam(required = false) Date gmtEndTime) {


        //  获取数据
        List<StatisticSitePageViewsPO> siteViewCountList = sitePageViewsService.pageViewCount(gmtStartTime, gmtEndTime);

        ArrayList<BarChartVO> siteViewCountVOS = new ArrayList<>();
        for (StatisticSitePageViewsPO statisticSitePageViewsPO : siteViewCountList) {
            BarChartVO siteViewCountVO = new BarChartVO();
            siteViewCountVO.setName(statisticSitePageViewsPO.getSiteName());
            siteViewCountVO.setCount(statisticSitePageViewsPO.getArticleViewsCount() + statisticSitePageViewsPO.getSiteHomeViewsCount()
                    + statisticSitePageViewsPO.getColumnHomeViewsCount());
            siteViewCountVOS.add(siteViewCountVO);
        }

        BarChartListVO siteViewCountListVO = new BarChartListVO();
        siteViewCountListVO.setList(siteViewCountVOS);
        return R.success().data(siteViewCountListVO);
    }

    @ApiOperation(value = "站点访问量统计", notes = "站点访问量统计")
    @GetMapping("/site-view-count-day")
    public R<LineChartListVO> siteViewCountDay(
            @ApiParam(value = "开始时间（10位时间戳）", required = true, example = "1234567891") @RequestParam(required = false) Date gmtStartTime,
            @ApiParam(value = "结束时间（10位时间戳）", required = true, example = "1231231231") @RequestParam(required = false) Date gmtEndTime) {
        if (null == gmtStartTime) {
            return R.failureRequestParam().msg("开始时间不能为空");
        }
        if (null == gmtEndTime) {
            return R.failureRequestParam().msg("结束时间不能为空");
        }
        DateTime startTime = DateUtil.beginOfDay(gmtStartTime);
        DateTime endTime = DateUtil.endOfDay(gmtEndTime);

        // dayList
        ArrayList<String> dayList = new ArrayList<>();
        for (Date tempTime = startTime; tempTime.compareTo(endTime) <= 0; DateUtil.offsetDay(tempTime, 1)) {
            dayList.add(DateUtil.format(tempTime, ""));
        }

        // dataList
        QueryWrapper<StatisticSitePageViewsPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("GMT_DATE", startTime);
        queryWrapper.le("GMT_DATE", endTime);
        queryWrapper.groupBy("SITE_ID");
        queryWrapper.orderByAsc("GMT_DATE");
        List<StatisticSitePageViewsPO> list = sitePageViewsService.list(queryWrapper);

        Long siteId = list.get(0).getSiteId();
        SitePO sitePO = siteService.getById(siteId);
        for (StatisticSitePageViewsPO statisticSitePageViewsPO : list) {
            if (siteId.equals(statisticSitePageViewsPO.getSiteId())) {

            }
        }
        return R.success();
    }



}
